package com.jysz.certificate.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.beanutils.ConvertUtils;

/**
 * 核心工具类
 * @author xuxiaolong
 */
public class CommonUtil {
	
	private static Properties props;
	
	/**
	 * 获得config.properties中的属性
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {
		return props.getProperty(key);
	}
	
	static {
		props = new Properties();
		try {
			props.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("config.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	public static boolean isEmpty(String str){
		if(str==null||"".equals(str))
			return true;
		return false;
	}
	
	public static boolean isNotEmpty(String str){
		return !isEmpty(str);
	}
	
	@SuppressWarnings("rawtypes")
	public static boolean isEmptyList(List list){
		if(list==null||list.size()==0)
			return true;
		return false;
	}

	/**
	 * 根据","来将ids拆分成数组
	 * @param ids
	 * @return
	 */
	public static Long[] getLongAry(String ids){
		List<Long> list=getLongList(ids);
		if(list!=null&&list.size()>0){
			Long ary[]=new Long[list.size()];
			for(int i=0;i<list.size();i++){
				ary[i]=list.get(i);
			}
			return ary;
		}
		return null;
	}
	
	/**
	 * 根据","来将ids拆分成集合
	 * @param ids
	 * @return
	 */
	public static List<Long> getLongList(String ids){
		List<Long>list=null;
		if(isNotEmpty(ids)){
			ids=ids.trim();
			if(ids.endsWith(","))
				ids=ids.substring(0,ids.length()-1);
			String strs[]=ids.split(",");
			if(strs!=null&&strs.length>0){
				list=new ArrayList<Long>();
				for(int i=0;i<strs.length;i++){
					if(isNotEmpty(strs[i]))
						list.add(Long.parseLong(strs[i]));
				}
			}
		}
		return list;
	}
	
	/**
	 * 将num的前面用0补齐，直到长度达到length
	 * @param length 期望返回的字符串的长度
	 * @param num    
	 * @return
	 */
	public static String getAutoNum(int length,int num){
		
		return String.format("%1$0"+length+"d", num);
	}

	/**
	 * 集合转换成数组
	 * @param list
	 * @return
	 */
	public static String[] listToArray(List<String>list){
		if(list==null)
			return null;
		String [] ary=new String[list.size()];
		for(int i=0;i<list.size();i++)
			ary[i]=list.get(i);
		return ary;
	}
	
	/**
	 * 从ids中获得id数组
	 * @param ids 用,隔开
	 * @return
	 */
	public static Long[] getLongArrayFromIds(String ids,String split){
		if(isEmpty(ids)||isEmpty(split))
			return null;
		String ary[]=ids.split(split);
		if(ary==null)
			return null;
		return (Long[]) ConvertUtils.convert(ary, Long.class); 
	}
	
	public static String getStringFromArray(Long ary[]){
		
		if(ary!=null&&ary.length>0){
			StringBuilder sb=new StringBuilder();
			for(int i=0;i<ary.length;i++){
				sb.append(ary[i]);
				if(i!=ary.length-1)
					sb.append(",");
					
			}
			return sb.toString();
		}
		return null;
		
	}
}
